package client.ui.component.card;

import client.assets.Icons;
import client.ui.theme.ThisFont;
import client.ui.util.MaterialColors;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Card extends JPanel {
    private final int borderWidth = 2;
    protected Color hoverBorderColor = MaterialColors.RED_500;
    protected JLabel img;
    protected JLabel goodName;
    protected String imgUrl;
    protected int id;
    protected JLabel price;
    protected boolean hover;
    public Card(int id, String imgUrl, String goodName, double price){
        super();
        this.id = id;
        this.imgUrl = imgUrl;
        this.img = new PictureLabel(imgUrl,Icons.place_hold,SwingConstants.CENTER);
        this.goodName = new JLabel(goodName,SwingConstants.CENTER);
        this.price = new JLabel("￥ " + price,SwingConstants.CENTER);
        initial();
    }

    void initial(){
        this.setBackground(MaterialColors.WHITE);
        this.img.setMinimumSize(new Dimension(180,180));
        this.img.setMaximumSize(new Dimension(180,180));
        this.goodName.setFont(ThisFont.BIG_FONT);
        this.price.setFont(ThisFont.LARGE_FONT);
        this.price.setForeground(MaterialColors.COSMO_RED);

        GridBagConstraints gridBagConstraints;
        GridBagLayout gridBagLayout = new GridBagLayout();
        this.setLayout(gridBagLayout);

        this.add(img);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weighty = 10;
        gridBagConstraints.gridy = 0;
        gridBagLayout.addLayoutComponent(img,gridBagConstraints);

        this.add(goodName);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weighty = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new Insets(8,8,4,8);
        gridBagLayout.addLayoutComponent(goodName,gridBagConstraints);

        this.add(price);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weighty = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new Insets(0,8,12,8);
        gridBagLayout.addLayoutComponent(price,gridBagConstraints);

        this.setBorder(new EmptyBorder(borderWidth,borderWidth,borderWidth,borderWidth));
        this.setPreferredSize(new Dimension(220,300));
        //设置卡片的监听
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Card press,Id:" + id);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                hover = true;
                ((PictureLabel) Card.this.img).hover = true;
                repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                hover = false;
                ((PictureLabel) Card.this.img).hover = false;
                repaint();
            }
        });
    }

    @Override
    protected void paintBorder(Graphics g) {
        int offset = borderWidth / 2;
        super.paintBorder(g);
        if (hover){
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(hoverBorderColor);
            g2.setStroke(new BasicStroke(borderWidth));
            //上
            g2.drawLine(offset - 1,offset - 1,getWidth() - offset,offset - 1);
            //左
            g2.drawLine(offset - 1,offset,offset - 1,getHeight() - offset);
            //右
            g2.drawLine(getWidth() - offset,0,getWidth() - offset,getHeight());
            //下
            g2.drawLine(0,getHeight() - offset,getWidth(),getHeight() - offset);

            g2.dispose();
        }

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

    }

    public Color getHoverBorderColor() {
        return hoverBorderColor;
    }

    public void setHoverBorderColor(Color hoverBorderColor) {
        this.hoverBorderColor = hoverBorderColor;
    }

    public String getGoodName() {
        return goodName.getText();
    }

    public void setGoodName(String goodName) {
        this.goodName.setText(goodName);
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public int getId() {
        return id;
    }

    public double getPrice() {
        return Double.parseDouble(this.price.getText());
    }

    public void setPrice(double price) {
        this.price.setText("￥ " + price);
    }
}
